package com.sita.tboard.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.persistence.Route;
import com.sita.tboard.ui.activity.RoadRecordInformationDisplayActivity;
import com.sita.tboard.ui.view.SubListView;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangde001 on 2015/12/18.
 */
public class RoadDayAdapter extends BaseAdapter {
    public Context context;
    public ArrayList<String> arrayList_data;
    private List<Route> monthTracks;
    private long timeStart, timeEnd;
    private Route tracks;

    public RoadDayAdapter(Context context, ArrayList<String> arrayList_data, List<Route> monthTracks) {
        this.arrayList_data = arrayList_data;
        this.context = context;
        this.monthTracks = monthTracks;
    }

    @Override
    public int getCount() {
        return arrayList_data.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList_data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.roadrecord_expand_item, null);
            viewholder.textView = (TextView) convertView.findViewById(R.id.tv_item);
//            viewholder.trackListView = (SubListView) convertView.findViewById(R.id.road_record_list_track);
            viewholder.starttime = (TextView) convertView.findViewById(R.id.start_time);
            viewholder.endtime = (TextView) convertView.findViewById(R.id.tv_final_road);
            viewholder.imageView_start_location = (ImageView) convertView.findViewById(R.id.img_day_location);
            viewholder.layout_track_item = (RelativeLayout) convertView.findViewById(R.id.layout_track_item);
            viewholder.tv_road_mileage = (TextView) convertView.findViewById(R.id.tv_road_mileage);
            viewholder.tv_record_time = (TextView) convertView.findViewById(R.id.tv_record_time);
            viewholder.tv_record_speed = (TextView) convertView.findViewById(R.id.tv_record_speed);
            viewholder.tv_start_address = (TextView) convertView.findViewById(R.id.tv_start_address);
            viewholder.final_speed_road = (TextView) convertView.findViewById(R.id.final_speed_road);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        if (parent instanceof SubListView) {
            if (!((SubListView) parent).isOnMeasure) {
                tracks = monthTracks.get(position);
                if (tracks == null) {
                    return null;
                }
                long distance = tracks.getDistance();
                String mileage = formatDistance(distance) + "km";
                long starttime = tracks.getStartTime();
                long endtime = tracks.getEndTime();
                SimpleDateFormat dateFm = new SimpleDateFormat("HH:mm"); //格式化当前系统日期
                String datestarttime = dateFm.format(starttime);
                String dateendtime = dateFm.format(endtime);
                long timediff = endtime - starttime;//相差多少微秒
                long hour, min, s;
                String timeshow = "0";
                if ((timediff / 1000) < 60) {//小于一分钟
                    s = timediff / 1000;
                    timeshow = s + "s";
                } else if ((timediff / 1000) >= 60 && (timediff / 1000) < 3600) {//大于一分钟，小于一小时
                    min = timediff / (1000 * 60);
                    s = timediff / 1000 - min * 60;
                    timeshow = min + "m" + s + "s";
                } else if ((timediff / 1000) >= 3600) {
                    hour = timediff / (60 * 60 * 1000);
                    min = (timediff / (60 * 1000)) - hour * 60;
                    timeshow = hour + "h" + min + "m";
                }
                //平均速度
                float speed = (float) ((distance / (timediff / 1000)) * 3600);//速度为米每小时
                final String speedshow = formatDistance(speed) + "km/h";
                //路线的起始位置
                String startLocation = tracks.getStartAddress();
                String endLocation = tracks.getEndAddress();
                Log.e("时期位置信息", "开始" + startLocation + "结束" + endLocation);
                viewholder.starttime.setText(datestarttime);
                viewholder.endtime.setText(dateendtime);
                viewholder.tv_road_mileage.setText(mileage);
                viewholder.tv_record_time.setText(timeshow);
                viewholder.tv_record_speed.setText(speedshow);
                viewholder.tv_start_address.setText(startLocation);
                viewholder.final_speed_road.setText(endLocation);
                viewholder.imageView_start_location.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, RoadRecordInformationDisplayActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putLong("trackID", tracks.getId());
                        bundle.putString("avespeed", speedshow);
                        intent.putExtras(bundle);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                });
                viewholder.layout_track_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, RoadRecordInformationDisplayActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putLong("trackID", tracks.getId());
                        bundle.putString("avespeed", speedshow);
                        intent.putExtras(bundle);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                });
                viewholder.textView.setText(arrayList_data.get(position).toString());
//        ArrayList<String> arrayList_trackdata = new ArrayList<>();
//        arrayList_trackdata.add("只有一条");
//        Tracks tracks = monthTracks.get(position);
//        RoadTrackDataAdapter roadTrackDataAdapter = new RoadTrackDataAdapter(GlobalContext.getGlobalContext(), arrayList_trackdata, tracks);
//        viewholder.trackListView.setAdapter(roadTrackDataAdapter);

            }
        }
        return convertView;
    }

    private String formatDistance(float distance) {
        float distance_km = distance / 1000;
        String distance_result = "0";
        DecimalFormat decimalFormat;
        if (distance_km < 10) {
            decimalFormat = new DecimalFormat("0.00");
            distance_result = decimalFormat.format(distance_km);
        } else if (distance >= 10 && distance < 100) {
            decimalFormat = new DecimalFormat("0.0");
            distance_result = decimalFormat.format(distance_km);
        } else if (distance >= 100) {
            decimalFormat = new DecimalFormat("0");
            distance_result = decimalFormat.format(distance_km);
        }
        return distance_result;
    }

    class ViewHolder {
        TextView textView;
        //        ImageView imageView_start_location;
        ListView trackListView;
        TextView starttime, endtime, tv_road_mileage, tv_record_time, tv_record_speed, tv_start_address, final_speed_road;
        ImageView imageView_start_location;
        RelativeLayout layout_track_item;
    }
}
